package three;
import java.util.*;
public class A044 {





    //199.二叉树的右视图

    //思路：层序遍历，记录最后面一个节点即可
    List<Integer> list = new ArrayList<>();
    public List<Integer> rightSideView(TreeNode root) {
        if(root==null) return list;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while(!queue.isEmpty()) {
            int size = queue.size();
            for(int i=0;i<size;i++) {
                TreeNode tmp = queue.poll();
                if(tmp.left!=null) queue.add(tmp.left);
                if(tmp.right!=null) queue.add(tmp.right);
                if(i==size-1) list.add(tmp.val);
            }
        }
        return list;
    }

}
